/**
 * 版本兼容性声明配置
 */
interface VersionCompatibility {
  [module: string]: {
    [version: string]: {
      compatibleWith: string[];
      dependencies: Record<string, string>;
    };
  };
}

export const versionCompatibility: VersionCompatibility = {
  core: {
    v1: {
      compatibleWith: ['adapters/v1', 'platform/v1'],
      dependencies: {
        '@adapters': 'v1',
        '@platform': 'v1'
      }
    },
    v2: {
      compatibleWith: ['adapters/v2', 'platform/v1'],
      dependencies: {
        '@adapters': 'v2',
        '@platform': 'v1'
      }
    }
  },
  adapters: {
    v1: {
      compatibleWith: ['core/v1'],
      dependencies: {
        '@core': 'v1'
      }
    },
    v2: {
      compatibleWith: ['core/v2'],
      dependencies: {
        '@core': 'v2'
      }
    }
  },
  platform: {
    v1: {
      compatibleWith: ['core/v1', 'core/v2'],
      dependencies: {
        '@core': '>=v1'
      }
    }
  },
  wasm: {
    v1: {
      compatibleWith: ['core/v2'],
      dependencies: {
        '@core': 'v2'
      }
    }
  }
};

/**
 * 检查版本兼容性
 */
export function checkCompatibility(module: string, version: string, targetModule: string, targetVersion: string): boolean {
  const compatInfo = versionCompatibility[module]?.[version];
  if (!compatInfo) return false;

  return compatInfo.compatibleWith.includes(`${targetModule}/${targetVersion}`) ||
    compatInfo.dependencies[`@${targetModule}`] === targetVersion ||
    (compatInfo.dependencies[`@${targetModule}`]?.startsWith('>=') && 
     targetVersion >= compatInfo.dependencies[`@${targetModule}`].slice(2));
}